<?php


namespace App\Service;


use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Library\Common;
use App\Model\FlyGourmetMealConf;
use App\Service\ShopConfigService;
use Hyperf\Di\Annotation\Inject;


/**
 * 商户美食点餐配置
 * Class FlyGourmetMealConfService
 * @package App\Service
 */
class FlyGourmetMealConfService extends BaseService
{

    /**
     * @Inject
     * @var ShopConfigService
     */
    private $shopConfig;
    /**
     * 保存配置
     * saveing
     * @param int $shopId
     * @param array $args
     * @return \Hyperf\Utils\HigherOrderTapProxy|int|mixed|void
     * date:2022/7/19
     * time:15:43
     * auth：xyc
     */
    public function saveing(int $shopId, array $args): int
    {
        $MealConf = New FlyGourmetMealConf();
        $flyGourmetMealConf = $MealConf->where('shop_id', $shopId)->first();

        if(isset($args['open_time'])){
            if($args['open_time']!=[]){
                $open_time = $args['open_time'];
                $param['open_time'] = json_encode($args['open_time']);
                $check_res  = $this->shopConfig->check_time($open_time);
                if($check_res){
                    $this->shopConfig->set_open_time(intval($shopId),$open_time);
                }else{
                    throw new \RuntimeException("时间格式不正确！", ErrorCode::SERVER_ERROR);
                }
            }
        }
        //初始化
        if ($flyGourmetMealConf == null) {
            $MealConf->shop_id = $shopId;
            $MealConf->consume_type =$args['consume_type'];
            $MealConf->open_time = $args['open_time'];
            $MealConf->is_reservation = $args['is_reservation'];
            if (!$MealConf->save()) {
                throw new BusinessException(ErrorCode::SERVER_ERROR, "保存配置失败!");
            }
            return (int)$MealConf->id;
        }else{
            $flyGourmetMealConf->shop_id = $flyGourmetMealConf->shop_id;
            $flyGourmetMealConf->consume_type = $args['consume_type'];
            $flyGourmetMealConf->is_reservation = $args['is_reservation'];
            $flyGourmetMealConf->open_time = $args['open_time'];
            if (!$flyGourmetMealConf->save()) {
                throw new BusinessException(ErrorCode::SERVER_ERROR, "保存配置失败!");
            }
            return (int)$flyGourmetMealConf->id;
        }
    }

    public function info(int $shop_id)
    {
        return FlyGourmetMealConf::query()
            ->where('shop_id',$shop_id)
            ->with('shop_info')
            ->first();
    }

//    获取支持订餐的店铺
    public function get_meal_shop()
    {
        return FlyGourmetMealConf::query()
            ->where('is_reservation',1)
            ->pluck('shop_id')->toArray();
    }
}